$('#table_list').DataTable({
    dom: '<"top">rt<"bottom"flip><"clear">',
    language: {
        "sUrl":'../static/plugins/datatables/language.json'
    },
    "searching": false,
    "processing": true,
    "serverSide": true,
    ajax:{
        url:'/wish-single-report/get-wish-single-report',
        type:'post',
        data:function(d){
        	d.cust_member_no = $('#cust_member_no').val();
        	d.use_flg = $('#use_flg').val();
        	d.reserv_name = $('#reserv_name').val();
        	d.reserv_mobile = $('#reserv_mobile').val();
        },
        error:getError
    },
    aoColumns: [
        { "data": "id", "bSortable": false},
        { "data": "id", "bSortable": false},
        { "data": "cust_member_no", "bSortable": false },
        { "data": "create_time", "bSortable": false },
        { "data": "reserv_name", "bSortable": false },
        { "data": "reserv_mobile", "bSortable":false },
        { "data": "reserv_time", "bSortable":false },
        { "data": "reserv_time_ex", "bSortable":false },
        { "data": "wish_list_id", "bSortable":false },
        { "data": "use_flg", "bSortable":false },
        { "data": "id", "bSortable":false },
        { "data": "id", "bSortable":false }
    ],
    columnDefs: [
        {
            "targets": [0],
            "orderable":false,
            "searchable":false,
            "render": function(data, type, full) {
            	return '<input name="check_all" type="checkbox" value="'+data+'"/>';
            }
        },
        {
            "targets": [8],
            "orderable":false,
            "searchable":false,
            "render": function(data, type, full) {
            	return '<a class="select_goods" data-id="'+data+'" href="javascript:void()">查看商品</a>';
            }
        },
        {
            "targets": [9],
            "orderable":false,
            "searchable":false,
            "render": function(data, type, full) {
            	return '<span class="switch" data-id="'+ full.id +'" data-flg="'+(data == 1 ? 0 : 1)+'"><input  style="width:20px;height:20px;" type="checkbox" '+(data == 1 ? 'checked="checked"' : '')+' class="input-check"></span>';
            }
        },
        {
            "targets": [10],
            "orderable":false,
            "searchable":false,
            "render": function(data, type, full) {
            	return '<a class="select_contact_record" data-id="'+data+'" href="javascript:void()">记录</a>';
            }
        },
        {
            "targets": [11],
            "orderable":false,
            "searchable":false,
            "render": function(data, type, full) {
            	return '<a href="/wish-single-report/log?share_wish_reserv_id='+data+'">日志</a>';
            }
        }
    ]
});

$('input[name="check_all"]').on('click',function(){
	$('input[name=check_all]').prop('checked',$(this).prop('checked'));
});

$('#reset_button').on('click',function(){
	$('#cust_member_no').val('');
	$('#use_flg').val(0);
	$('#reserv_name').val('');
	$('#reserv_mobile').val('');
	table_list_reload();
});

function table_list_reload(){
	var table = $('#table_list').DataTable();
    table.ajax.reload();
}

$('table').on('click','.select_goods',function(){
	var share_wish_reserv_id = $(this).attr('data-id');
	$.post('/wish-single-report/get-goods',{share_wish_reserv_id:share_wish_reserv_id},success,'json');
    return false;
    function success(data){
        $.jBox.close();
        if(data.code == 200){
        	$('#wish-goods-modal').modal('show');
            var result = data.val;
			var html = '';
			if(result.length > 0){
				result.forEach(function(e,index){
					var length = e.activity_info.length > e.activity_new.length ? e.activity_info.length : e.activity_new.length;
					var span = length > 0 ? length : 1;
					html += '<tr>';
					html += '<td rowspan="'+span+'">'+(index+1)+'</td>';
					html += '<td rowspan="'+span+'">'+e.sku+'</td>';
					html += '<td rowspan="'+span+'">'+e.goods_name_cn+'</td>';
					html += '<td rowspan="'+span+'">'+e.create_time+'</td>';
					html += '<td rowspan="'+span+'">'+(e.use_flg == '1' ? '有效' : '无效')+'</td>';
					if(e.activity_info.length > 0){
						var old_activity = e.activity_info[0];
						html += '<td>'+old_activity.id+'</td>';
						html += '<td>'+old_activity.activity_identification+'</td>';
					}else{
						html += '<td>--</td>';
						html += '<td>--</td>';
					}
					html += '<td rowspan="'+span+'">'+(e.use_flg_new == '1' ? '有效' : '无效')+'</td>';
					if(e.activity_new.length > 0){
						var new_activity = e.activity_new[0];
						html += '<td>'+new_activity.id+'</td>';
						html += '<td>'+new_activity.activity_identification+'</td>';
					}else{
						html += '<td>--</td>';
						html += '<td>--</td>';
					}
					html += '</tr>';
					if(length > 1)for(var i=1;i<length;i++){
						html += '<tr>';
						var old_activity = e.activity_info[i];
						var new_activity = e.activity_new[i];
						if(old_activity != undefined){
							html += '<td>'+old_activity.id+'</td>';
							html += '<td>'+old_activity.activity_identification+'</td>';
						}else{
							html += '<td>--</td>';
							html += '<td>--</td>';
						}
						if(new_activity != undefined){
							html += '<td>'+new_activity.id+'</td>';
							html += '<td>'+new_activity.activity_identification+'</td>';
						}else{
							html += '<td>--</td>';
							html += '<td>--</td>';
						}
						html += '</tr>';
					}
				});
			}
			$('#wish-goods-modal').find('input[name=id]').val(share_wish_reserv_id);
			$('#table_goods_list').find('tbody').html(html);
        }else{
            $.jBox.error(data.val, '错误');
        }
    }
	
});

function changeWishSingleReportStatus(id,use_flg){
	title = '确定该心愿单标记为未处理？';
	if(use_flg == 1){
		title = '确定该心愿单标记为已处理？';
	}
	$.jBox.confirm(title, "提示", function(v){
        if (v == 'ok'){
            $.post('/wish-single-report/change-wish-single-report-status',{id:id,use_flg:use_flg},success,'json');
            return false;
            function success(data){
                $.jBox.close();
                if(data.code == 200){
                    $.jBox.tip('处理成功', 'success');
                    window.setTimeout(function () { table_list_reload(); }, 1000);
                }else{
                    $.jBox.error(data.val, '错误');
                }
            }
        }else{
            $.jBox.close();
            if(use_flg == 1){
        		$('#table_list').find('span[data-id='+id+']').children('input').prop('checked',false);
        	}else{
        		$('#table_list').find('span[data-id='+id+']').children('input').prop('checked',true);
        	}
        }
    });
}

$('table').on('click','.select_contact_record',function(){
	var share_wish_reserv_id = $(this).attr('data-id');
	$.post('/wish-single-report/get-wish-single-report-record',{share_wish_reserv_id:share_wish_reserv_id},success,'json');
    return false;
    function success(data){
        $.jBox.close();
        if(data.code == 200){
            $('#wish-single-report-modal').modal('show');
            var result = data.val;
			var html = '<label for="tag_group_id_add">联系记录</label>';
			if(result.length > 0){
				result.forEach(function(e,index){
					var style = 'border-bottom:2px solid #999;';
					if(index == result.length-1){
						style = '';
					}
					html += '<div class="col-sm-12 control-label" style="'+style+'">';
					html += '<p>'+e.oper_time+' '+e.oper+'</p>';
					html += '<p>'+e.record+'</p>';
					html += '</div>';
				});
			}
			$('#wish-single-report-modal').find('input[name=id]').val(share_wish_reserv_id);
			$('#wish-single-report-modal').find('.old_log').html(html);
			$('#record').val('');
        }else{
            $.jBox.error(data.val, '错误');
        }
    }
});

$('#wish-single-report-modal').on('click','.ajax_post_tag',function(){
	var record = $('#record').val();
	if(record == ''){
		$.jBox.error('联系记录不可为空', '错误');
		return false;
	}
	$(this).attr('disabled',true);
	var form = $('#wish_single_report_form');
	var where = $(this);
	$.post(form.attr('action'),form.serialize(),success,'json');
    return false;
	function success(data){
        if(data.code == 200)
        {
            $.jBox.tip('处理成功', 'success');
            window.setTimeout(function () {
                $('#wish-single-report-modal').modal('hide');
                $(where).attr('disabled',false);
                window.setTimeout(function () { table_list_reload(); }, 1000);
            }, 1000);
        } else{
            $.jBox.error(data.val, '错误');
        }
	}
});

$('table').on('click','.input-check',function(){
	var id = $(this).parent().attr('data-id');
	var use_flg = $(this).parent().attr('data-flg');
	changeWishSingleReportStatus(id,use_flg);
});

function exportWishSingleReport(){
	var share_wish_reserv_id = [];
	$('input[name=check_all]:checked').each(function(){
		if($(this).val() != ''){
			share_wish_reserv_id.push($(this).val());
		}
	});
	if(share_wish_reserv_id == ''){
		$.jBox.error('请选择需要导出的预约信息', '错误');
		return false;
	}
	$('input[name=share_wish_reserv_id]').val(share_wish_reserv_id);
	$('#wish-single-report-select').submit();
}